using System;
using System.IO;
using System.Diagnostics;

namespace TradeSea
{
#if WINDOWS || XBOX
    static class Program
    {
        public static  string GetAppDir()
        {
            return Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            TradeSeaShared.Singiltons.Init(args.Length == 1 && args[0] == "-l");
            if (TradeSeaShared.Singiltons.RunServer)
            {
                TradeSeaServer.RunServer();
            }
            if (args.Length == 2 && args[0] == "-c")
            {
                TradeSeaNetClient.NetClientHostName = args[1];
            }

            //Load or new?
            Console.WriteLine("Load or New Game? (l,n)");
            string y = Console.ReadLine();
            bool wait = true;
            string path = GetAppDir();
            string filename;
            bool done = false;

            bool fullScreen = false;
            string playerName = "";
            Inventory playerInventory = new Inventory();
            SaveGame saveGame = new SaveGame();

            while (wait == true)
            {
                if (y == "l")
                {
                    Console.WriteLine("Please enter player name");
                    playerName = Console.ReadLine();
                    filename = path + "\\" + playerName + ".txt";

                    try
                    {
                        playerInventory = saveGame.Open(filename);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        playerInventory.addToInventory("basic net");
                    }
                    wait = false;
                }
                else if (y == "n")
                {
                    Console.WriteLine("Please enter a new player name");
                    playerName = Console.ReadLine();
                    filename = path + "\\" + playerName + ".txt";
                    playerInventory.addToInventory("basic net");
                    wait = false;
                }
                else if (y == "p")
                { wait = false; done = true; }
                else
                {
                    Console.WriteLine("Please enter a valid input");
                    y = Console.ReadLine();
                }
            }
            //fullscreen?
            if (done == false)
            {
                Console.WriteLine("FullScreen Mode? (y,n)");
                string x = Console.ReadLine();

                while (done == false)
                {
                    if (x == "y")
                    {
                        fullScreen = true;
                        done = true;
                    }
                    else if (x == "n")
                    {
                        done = true;
                    }
                    else
                    {
                        Console.WriteLine("Please enter a valid input");
                        x = Console.ReadLine();
                    }
                }
            }

            TradeSeaNetClient.Init();
            using (MainGame game = new MainGame(fullScreen, playerName, playerInventory, saveGame))
            {
                game.Run();
            }
        }
    }
#endif
}

